package edu.wpi.cs509.ui;

import edu.wpi.cs509.representation.Location;
import edu.wpi.cs509.representation.PlayerPiece;
import edu.wpi.cs509.ui.intfc.GameEventNotify;

/**
 * The GameEventHandler class implements the event handler for processing
 * asynchronous game events from the client logic.
 *  
 * @author afoltan
 *
 */
public class GameEventHandler implements GameEventNotify {
	
	GameWindow gameBoardWindow;
	
	/**
	 * Constructor for the game event handler
	 */
	public GameEventHandler( GameWindow gameBoardWindow ) {
		this.gameBoardWindow = gameBoardWindow;
	}

	/**
	 * Trace support
	 */
	private void trace(String func) {
		System.out.println("UI <----- "+func+" ----- ClientLogic");
	}

	@Override
	public void notifyPieceMoved(PlayerPiece piece, Location from, Location to) {
		trace("notifyPieceMoved()");
		
		if ((piece == null) || (from == null) || (to == null)) {
			System.out.println("ERROR:: notifyPieceMoved(notifyPieceMoved(PlayerPiece piece, Location from, Location to)");
			if (piece == null) {
				System.out.println("ERROR:: 'piece' argument passed as null)");
			}
			if (from == null) {
				System.out.println("ERROR:: 'from' argument passed as null)");
			}
			if (to == null) {
				System.out.println("ERROR:: 'to' argument passed as null)");
			}
			System.out.println("ERROR:: ignoring message");
			return;
		}
		
		gameBoardWindow.movePiece(piece,from, to);
		gameBoardWindow.haltListeners();
	}

	@Override
	public void notifyRefreshGameBoard() {
		trace("notifyRefreshGameBoard()");
		gameBoardWindow.getGameWindowFrame().redraw();
	}

	@Override
	public void notifyAttack(PlayerPiece attacker, Location from, PlayerPiece defender, Location to) {
		trace("notifyAttack()");
		
		if ((attacker == null) || (from == null) || (defender == null) || (to == null)) {
			System.out.println("ERROR:: notifyAttack(PlayerPiece attacker, Location from, PlayerPiece defender, Location to)");
			if (attacker == null) {
				System.out.println("ERROR:: 'attacker' argument passed as null)");
			}
			if (from == null) {
				System.out.println("ERROR:: 'from' argument passed as null)");
			}
			if (defender == null) {
				System.out.println("ERROR:: 'defender' argument passed as null)");
			}
			if (to == null) {
				System.out.println("ERROR:: 'to' argument passed as null)");
			}
			System.out.println("ERROR:: ignoring message");
			return;
		}

		gameBoardWindow.attackPiece(attacker, from, defender, to);
	}

	@Override
	public void notifyGameStarted() {
		trace("notifyGameStarted()");
		gameBoardWindow.gameStarted();
	}

	@Override
	public void notifyGameOver(boolean winner) {
		trace("notifyGameOver()");
		gameBoardWindow.gameOver(winner);
	}

	@Override
	public void notifyOpponentQuit() {
		trace("notifyOpponentQuit()");
		gameBoardWindow.opponentQuit();
	}

	@Override
	public void notifyItsYourTurn() {
		trace("notifyItsYourTurn()");
		gameBoardWindow.myTurn();
	}

	@Override
	public void notifyPiecePlaced(PlayerPiece piece, Location location) {
		trace("notifyPiecePlaced()");
		
		if ((piece == null) || (location == null)) {
			System.out.println("ERROR:: notifyPiecePlaced(PlayerPiece piece, Location location)");
			if (piece == null) {
				System.out.println("ERROR:: 'piece' argument passed as null)");
			}
			if (location == null) {
				System.out.println("ERROR:: 'location' argument passed as null)");
			}
			System.out.println("ERROR:: ignoring message");
			return;
		}

		gameBoardWindow.placePiece(piece, location);
	}

	@Override
	public void notifyOpponentJoined() {
		trace("notifyOpponentJoined()");
		gameBoardWindow.opponentJoined();
	}

	@Override
	public void notifyOpponentReady() {
		trace("notifyOpponentJoined()");
		gameBoardWindow.opponentReady();
	}

	@Override
	public void notifyFailure(FailureReason reason) {
		trace("notifyFailure()");

		switch (reason) {
		case IllegalMove:
			// TODO
			break;
		default: break;				
		}
	}

	@Override
	public void notifyPieceRemoved(PlayerPiece piece, Location from) {
		trace("notifyPieceRemoved");
		
		if ((piece == null)) {
			System.out.println("ERROR:: notifyPiecePlaced(PlayerPiece piece, Location location)");
			if (piece == null) {
				System.out.println("ERROR:: 'piece' argument passed as null)");
			}
			if (from == null) {
				System.out.println("ERROR:: 'from' argument passed as null)");
			}
			System.out.println("ERROR:: ignoring message");
			return;
		}
		gameBoardWindow.removePiece(piece,from);
	}


}
